package net.kts.placebook.venue;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;

import net.kts.main.R;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class NearbyAdapter extends BaseAdapter {

	private ArrayList<Venue> mVenueList;
	private LayoutInflater mInflater;
	private HashMap<String, Bitmap> maps;
//	private Handler handler;
	public NearbyAdapter(Context c) {
		mInflater = LayoutInflater.from(c);
		this.maps = new HashMap<String, Bitmap>();
//		handler = new Handler();
	}

	public void setData(ArrayList<Venue> poolList) {
		mVenueList = poolList;
	}

	@Override
	public int getCount() {
		return mVenueList.size();
	}

	@Override
	public Object getItem(int position) {
		return mVenueList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		//		if(convertView !=null){
		//			return convertView;
		//		}

		ViewHolder holder;

		if (convertView == null) {
			convertView	=  mInflater.inflate(R.layout.pb_nearby_row, null);

			holder = new ViewHolder();

			holder.mNameTxt 		= (TextView) convertView.findViewById(R.id.tv_name);
			holder.mAddressTxt 		= (TextView) convertView.findViewById(R.id.tv_address);
			holder.mHereNowTxt 		= (TextView) convertView.findViewById(R.id.tv_here_now);
			holder.mDistanceTxt 	= (TextView) convertView.findViewById(R.id.tv_distance);
			holder.mRibbonImg		= (ImageView) convertView.findViewById(R.id.iv_ribbon);
			holder.iconImg 			= (ImageView) convertView.findViewById(R.id.icon);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		Venue venue 	= mVenueList.get(position);


		holder.mNameTxt.setText(venue.name);
		holder.mAddressTxt.setText(venue.address);
		holder.mDistanceTxt.setText(formatDistance(venue.distance));
		holder.mHereNowTxt.setText(venue.transName);

		// 아이콘 이미지 넣기
		//		Bitmap iconBitmap = GetImageFromURL(venue.iconUrl);
		//		holder.iconImg.setImageBitmap(iconBitmap);
		//		handler.post(new GetImageFromURL(venue.iconUrl, holder.iconImg));

		holder.iconImg.setImageResource(getIconId(venue.iconUrl));

		holder.mRibbonImg.setVisibility((venue.type.equals("trending")) ? View.VISIBLE : View.INVISIBLE);

		return convertView;
	}
	
	public int getIconId(String iconUrl){
		int id = 0;
		if(iconUrl.equals("https://foursquare.com/img/categories/building/default.png")){
			id = R.drawable.ico_pb_building_default;
		}else if(iconUrl.equals("https://foursquare.com/img/categories/food/coffeeshop.png")){
			id = R.drawable.ico_pb_coffeeshop;
		}else if(iconUrl.equals("https://foursquare.com/img/categories/food/cafe.png")){
			id = R.drawable.ico_pb_cafe;
		}else if(iconUrl.equals("https://foursquare.com/img/categories/food/korean.png")){
			id = R.drawable.ico_pb_korean;
		}else if(iconUrl.equals("https://foursquare.com/img/categories/food/chinese.png")){
			id = R.drawable.ico_pb_chinese;
		}else if(iconUrl.equals("https://foursquare.com/img/categories/food/ramen.png")){
			id = R.drawable.ico_pb_ramen;
		}else if(iconUrl.equals("https://foursquare.com/img/categories/food/asian.png")){
			id = R.drawable.ico_pb_asian;
		}else if(iconUrl.equals("https://foursquare.com/img/categories/food/burger.png")){
			id = R.drawable.ico_pb_burger;
		}else if(iconUrl.equals("https://foursquare.com/img/categories/food/bakery.png")){
			id = R.drawable.ico_pb_bakery;
		}else if(iconUrl.equals("https://foursquare.com/img/categories/food/bbq.png")){
			id = R.drawable.ico_pb_bbq;
			
			
		}else if(iconUrl.equals("https://foursquare.com/img/categories/nightlife/karaoke.png")){
			id = R.drawable.ico_pb_karaoke;
		}else if(iconUrl.equals("https://foursquare.com/img/categories/nightlife/pub.png")){
			id = R.drawable.ico_pb_pub;
		}else if(iconUrl.equals("https://foursquare.com/img/categories/arts_entertainment/arcade.png")){
			id = R.drawable.ico_pb_arcade;
		}else if(iconUrl.equals("https://foursquare.com/img/categories/arts_entertainment/default.png")){
			id = R.drawable.ico_pb_arts_entertainment_default;
			
		}else if(iconUrl.equals("https://foursquare.com/img/categories/shops/giftshop.png")){
			id = R.drawable.ico_pb_giftshop;
		}else if(iconUrl.equals("https://foursquare.com/img/categories/shops/jewelry.png")){
			id = R.drawable.ico_pb_jewelry;
		}else if(iconUrl.equals("https://foursquare.com/img/categories/shops/apparel_shoestore.png")){
			id = R.drawable.ico_pb_apparel_shoestore;
		}else if(iconUrl.equals("https://foursquare.com/img/categories/shops/bookstore.png")){
			id = R.drawable.ico_pb_bookstore;
			
			
		}else if(iconUrl.equals("https://foursquare.com/img/categories/travel/busstation.png")){
			id = R.drawable.ico_pb_busstation;
		}else if(iconUrl.equals("https://foursquare.com/img/categories/nightlife/beergarden.png")){
			id = R.drawable.ico_pb_beergarden;
		}else if(iconUrl.equals("https://foursquare.com/img/categories/education/default.png")){
			id = R.drawable.ico_pb_education_default;

		}else if(iconUrl.equals("https://foursquare.com/img/categories/nightlife/wine.png")){
			id = R.drawable.ico_pb_wine;

		}else if(iconUrl.equals("https://foursquare.com/img/categories/travel/boat.png")){
			id = R.drawable.ico_pb_boat;
		}else if(iconUrl.equals("https://foursquare.com/img/categories/parks_outdoors/harbor.png")){
			id = R.drawable.ico_pb_harbor;
		}else if(iconUrl.equals("https://foursquare.com/img/categories/arts_entertainment/bowling.png")){
			id = R.drawable.ico_pb_bowling;
		}else if(iconUrl.equals("https://foursquare.com/img/categories/travel/hotel.png")){
			id = R.drawable.ico_pb_hotel;
		}else if(iconUrl.equals("https://foursquare.com/img/categories/food/streetfood.png")){
			id = R.drawable.ico_pb_streetfood;
		}else if(iconUrl.equals("https://foursquare.com/img/categories/shops/financial.png")){
			id = R.drawable.ico_pb_financial;
		}else if(iconUrl.equals("https://foursquare.com/img/categories/parks_outdoors/campground.png")){
			id = R.drawable.ico_pb_campground;
		}else if(iconUrl.equals("https://foursquare.com/img/categories/parks_outdoors/beach.png")){
			id = R.drawable.ico_pb_beach;
		}else if(iconUrl.equals("https://foursquare.com/img/categories/travel/lightrail.png")){
			id = R.drawable.ico_pb_lightrail;
		}else if(iconUrl.equals("https://foursquare.com/img/categories/parks_outdoors/default.png")){
			id = R.drawable.ico_pb_parks_outdoors_default;
		}else if(iconUrl.equals("https://foursquare.com/img/categories/building/medical_hospital.png")){
			id = R.drawable.ico_pb_medical_hospital;
		}else if(iconUrl.equals("https://foursquare.com/img/categories/shops/technology.png")){
			id = R.drawable.ico_pb_technology;
		}else if(iconUrl.equals("https://foursquare.com/img/categories/nightlife/divebar.png")){
			id = R.drawable.ico_pb_divebar;
		}else if(iconUrl.equals("https://foursquare.com/img/categories/parks_outdoors/playground.png")){
			id = R.drawable.ico_pb_playground;
		}else{
			id = R.drawable.ico_pb_building_default;
		}
		return id;
	}


	final class GetImageFromURL implements Runnable{
		private String strImageURL;
		private ImageView iconImg;

		public GetImageFromURL(String strImageURL, ImageView iconImg) {
			super();
			this.strImageURL = strImageURL;
			this.iconImg = iconImg;
		}

		@Override
		public void run() {
			Log.i("fs333", "아이콘"+maps.containsKey(strImageURL));
			if(maps.containsKey(strImageURL)){
				iconImg.setImageBitmap(maps.get(strImageURL));
				Thread.currentThread().interrupt();
			}

			Bitmap imgBitmap = null;
			BufferedInputStream bis = null;

			URL url;
			try {
				url = new URL(strImageURL);

				URLConnection conn = url.openConnection();
				conn.connect();

				int nSize = conn.getContentLength();
				bis = new BufferedInputStream(conn.getInputStream(), nSize);
				imgBitmap = BitmapFactory.decodeStream(bis);


				bis.close();

			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				try {
					if(bis != null){
						bis.close();
					}
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}	

			}
			maps.put(strImageURL, imgBitmap);	
			iconImg.setImageBitmap(imgBitmap);
		}

	}

	private String formatDistance(double distance) {
		String result = "";

		DecimalFormat dF = new DecimalFormat("00");

		dF.applyPattern("0.#");

		if (distance < 1000)
			result = dF.format(distance) + " m";
		else {
			distance = distance / 1000.0;
			result   = dF.format(distance) + " km";
		}

		return result;
	}
	static class ViewHolder {
		TextView mNameTxt;
		TextView mAddressTxt;
		TextView mHereNowTxt;
		TextView mDistanceTxt;
		ImageView mRibbonImg;
		ImageView iconImg;
	}

}
